package com.dxc.untils.result;

import com.alibaba.fastjson.JSONObject;

public class ResultUtil<T> {
    private long code;
    private String msg;
    private T data;

    protected ResultUtil() {
    }

    protected ResultUtil(long code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> ResultUtil<T> success(T data) {
        return new ResultUtil<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  message 提示信息
     */
    public static <T> ResultUtil<T> success( String message,T data) {
        return new ResultUtil<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     * @param code 错误码
     * @param message 错误信息
     */
    public static <T> ResultUtil<T> failed(long code, String message) {
        return new ResultUtil<T>(code, message, null);
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> ResultUtil<T> failed(String message) {
        return new ResultUtil<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 匿名用户访问
     * @return
     */
    public static <T> ResultUtil<T> noLoginFailed() {
        return new ResultUtil<T>(ResultCode.NO_LOGIN.getCode(),ResultCode.NO_LOGIN.getMsg(),null);
    }
    /**
     * 用户权限不够
     * @return
     */
    public static <T> ResultUtil<T> noAuthFailed() {
        return new ResultUtil<T>(ResultCode.NO_AUTH.getCode(),ResultCode.NO_AUTH.getMsg(),null);
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return msg;
    }

    public void setMessage(String message) {
        this.msg = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
